<!--
 * @Description: What's this for
 * @Autor: WangYuan
 * @Date: 2021-07-07 17:08:36
 * @LastEditors: wang pingqi
 * @LastEditTime: 2022-07-11 10:57:12
-->
<template>
  <div class="all-checkbox">
    <div class="head">
      <h4>{{ title }}</h4>
      <el-checkbox
        :indeterminate="isIndeterminate"
        v-model="checkAll"
        @change="handleCheckAllChange"
      >全选</el-checkbox>
    </div>
    <div style="margin: 15px 0;"></div>

    <div class="exhibition-b">
      <span
        class="title"
        v-for="num, index in [1, 2, 3]"
        :key="index"
      >服务项目</span>
      <el-checkbox-group v-model="checkedList">
        <el-checkbox
          v-for="item in list"
          :label="item.code"
          :key="item.code"
          :style="{'display': item.name ? '' : 'none'}"
        >
          <div class="selfLabel">{{item.name}}</div>
        </el-checkbox>
      </el-checkbox-group>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      default: () => [],
    },
    title: {
      type: String,
    },
  },
  name: "all-checkbox",
  data() {
    return {
      list_: [],
      checkAll: false,
      checkedList: [],
      isIndeterminate: false,
    };
  },
  watch: {
    list: {
      immediate: true,
      handler() {
        this.list_ = this.list;

        this.checkedList = this.list_
          .filter((item) => item.value == 1)
          .map((item) => item.code);

        this.handleCheckedCitiesChange();
      },
    },
    checkedList: {
      immediate: true,
      handler() {
        this.list.map((item) => (item.value = "0"));
        this.list.map((item) => {
          if (this.checkedList.includes(item.code)) {
            item.value = "1";
          }
        });
        this.$emit("update:list", this.list);
      },
    },
  },
  methods: {
    handleCheckAllChange(val) {
      this.checkedList = val ? this.list.map((item) => item.code) : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange() {
      let checkedCount = this.checkedList.length;
      this.checkAll = checkedCount === this.list.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.list.length;
    },
  },
};
</script>

<style lang="scss" scoped>
.head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 25px;
}

.exhibition-b {
  border: 1px solid #ebeef5;
  .title {
    display: inline-block;
    width: 33.33%;
    height: 48px;
    line-height: 48px;
    text-align: center;
    &:nth-child(-n + 3) {
      background: rgba(204, 204, 204, 0.1);
    }
  }
  .el-checkbox-group {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }
  .el-checkbox {
    display: flex;
    align-items: center;
    width: 33.33%;
    min-height: 48px;
    line-height: 48px;
    text-align: center;
    white-space: inherit;
    margin-right: 0;
    padding-left: 27px;
    .selfLabel {
      width: 180px;
    }
  }
}
</style>